package comp512.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.border.EmptyBorder;

import comp512.classes.Player;


@SuppressWarnings("serial")
public class ClientGUI_Lobby extends JFrame{
	
	private Player player;
	private JList<String> sessionList;
	private static ClientGUI_Lobby instance = null;
	
	public ClientGUI_Lobby(){
		sessionList = new JList<>();
		setPlayer(Player.getInstance());
		player.setLobbyGUI(this);
		if(instance == null)
			instance = this;
		setMaximumSize(new Dimension(450, 300));
		setMinimumSize(new Dimension(450, 300));
		setPreferredSize(new Dimension(450, 300));
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				if(player != null)
					player.disconnect();
				System.exit(0);
			}
		});
		setTitle("Family Feud Game Lobby");
		
		JSplitPane spaneBase = new JSplitPane();
		spaneBase.setOrientation(JSplitPane.VERTICAL_SPLIT);
		spaneBase.setDividerLocation(0.25);
		getContentPane().add(spaneBase, BorderLayout.CENTER);
		
		JSplitPane spaneSessions = new JSplitPane();
		spaneSessions.setDividerLocation(0.4d);
		spaneBase.setRightComponent(spaneSessions);
		
		JSplitPane spaneGames = new JSplitPane();
		spaneGames.setOrientation(JSplitPane.VERTICAL_SPLIT);
		spaneGames.setResizeWeight(0.5);
		spaneSessions.setLeftComponent(spaneGames);
		
		JPanel panelGameHelp = new JPanel();
		spaneGames.setRightComponent(panelGameHelp);
		panelGameHelp.setLayout(new GridLayout(0, 1, 0, 0));
		
		JButton btnJoinSelectedGame = new JButton("<html><center>Join<br />Selected<br />Game</center></html>");
		btnJoinSelectedGame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "A demo session will now be started. This is not working until this message is removed and the game is retreived from the server.", "For Debugging Purposes Only", JOptionPane.WARNING_MESSAGE);
				ClientGUI_Session session = new ClientGUI_Session(player);
				session.setVisible(true);
			}
		});
		panelGameHelp.add(btnJoinSelectedGame);
		
		JButton btnJoinFromFile = new JButton("<html><center>Join<br />From<br />File<br /></center></html>");
		panelGameHelp.add(btnJoinFromFile);
		
		JButton btnCreateNewGame = new JButton("<html><center>Create<br />New<br />Game</center></html>");
		btnCreateNewGame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ClientGUI_CreateGame cg = new ClientGUI_CreateGame();				
				cg.setVisible(true);
			}
		});
		panelGameHelp.add(btnCreateNewGame);
		
		JPanel panelStatistics = new JPanel();
		spaneGames.setLeftComponent(panelStatistics);
		panelStatistics.setLayout(new BorderLayout(0, 0));
		
		JLabel lblStatistics = new JLabel();
		if(player != null){
			lblStatistics.setText("<html>" + player.getPlayerId() + "<br />Wins: " + player.getStatistics().getWins()
				+ "<br />Losses: " + player.getStatistics().getLosses() + "</html>");
		}
		lblStatistics.setBorder(new EmptyBorder(5, 5, 5, 5));
		panelStatistics.add(lblStatistics);
		
		JScrollPane scrollGameList = new JScrollPane();
		scrollGameList.setMinimumSize(new Dimension(5000, 5000));
		spaneSessions.setRightComponent(scrollGameList);
		scrollGameList.add(sessionList);
		
		JPanel panelServerInformation = new JPanel();
		spaneBase.setLeftComponent(panelServerInformation);
		panelServerInformation.setLayout(new BorderLayout(0, 0));
		
		JButton btnConnection = new JButton("Connect/Disconnect");
		panelServerInformation.add(btnConnection, BorderLayout.EAST);
		
		JLabel lblserverinfromation = new JLabel("<html>Server<br />Infromation</html>");
		lblserverinfromation.setBorder(new EmptyBorder(0, 5, 0, 5));
		panelServerInformation.add(lblserverinfromation, BorderLayout.CENTER);
		this.init();
	}
		
	private void init(){
	}
	
	//Getters
	public ClientGUI_Lobby getInstance(){
		return instance;
	}
		
	//Setters
	public void setPlayer(Player player) {
		this.player = player;	
	}
	
	public void setSessionList(DefaultListModel<String> model){
		sessionList = new JList<String>(model);
	}

	
}
